<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
require("../Vendor/twitteroauth.php");
App::uses('Controller', 'Controller');
session_start();
/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    public $helpers = array('Session', 'Html', 'Form');
    var $components = array('Session' => array('timeout' => 60),
        'Auth' => array(
            'authError' => "Access Denied",
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email')
                )
            )
            ));
    
    /**
     * this function gets the tattlr accounts from the db and sets them in the view before rendering the view.
     *and to get the pending tweets to appear as a notification for the moderator 
     * @return void
     * @author "Omar Ruedy","Youssef Madkour"
     */
    public function beforeRender() {

        $accountsArrayManager = ClassRegistry::init('Account')->find('all', array('fields' => array('id', 'screen_name', 'image'),
            'joins' =>
            array(
                array(
                    'table' => 'accounts_users',
                    'alias' => 'AccountsUsers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'account_id = id',
                ))),
            'conditions' => array('user_email' => $this->Auth->User('email'),
                'permission' => 'manager')
                ));
        $accountsArrayModerator = ClassRegistry::init('Account')->find('all', array('fields' => array('id', 'screen_name', 'image'),
            'joins' =>
            array(
                array(
                    'table' => 'accounts_users',
                    'alias' => 'AccountsUsers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'account_id = id',
                ))),
            'conditions' => array('user_email' => $this->Auth->User('email'),
                'permission' => 'moderator')
                ));
        $accountsArrayEditor = ClassRegistry::init('Account')->find('all', array('fields' => array('id', 'screen_name', 'image'),
            'joins' =>
            array(
                array(
                    'table' => 'accounts_users',
                    'alias' => 'AccountsUsers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'account_id = id',
                ))),
            'conditions' => array('user_email' => $this->Auth->User('email'),
                'permission' => 'editor')
                ));
        $currentlySlectedAccount = ClassRegistry::init('Account')->find('all', array('fields' => array('screen_name'), 'conditions' => array('id' => $this->Auth->User('current_account'))));
        $this->set('accountsArrayManager', $accountsArrayManager);
        $this->set('accountsArrayModerator', $accountsArrayModerator);
        $this->set('accountsArrayEditor', $accountsArrayEditor);
        $this->set('currentlySlectedAccount', $currentlySlectedAccount);
        $this->set('email', $this->Auth->User('email'));
        $this->set('current_account',$this->Auth->User('current_account'));



        $m = ClassRegistry::init('accounts_user')->find('all',array('conditions'=>array('Accounts_user.user_email'=>$this->Auth->User('email'),'permission'=>'moderator')));

        $r =  ClassRegistry::init('future_tweet')->find('all', array('conditions'=>array('status'=>'pending')));


        $r1 = array(); 
        $r2 = array(); 
        foreach ( $r AS $line ) { 
            if ( !in_array($line['future_tweet']['account_id'], $r2) ) { 
                $r2[] = $line['future_tweet']['account_id']; 
                $r1[] = $line; 
            } 
        } 
        $r = $r1; 
        unset($r1,$r2);
        


        $not =array();
        $k=0;
        for ($i = 0; $i < count($m); $i++){

            for ($j = 0; $j < count($r); $j++){

                if($m[$i]['accounts_user']['account_id']==$r[$j]['future_tweet']['account_id']){
                    $not[$k]=$r[$j];
                    $k++;
                }

            }

        }
    
        $this->set('notifications',$not);


        for ($i = 0; $i < count($not); $i++){
        $acc[$i] =ClassRegistry::init('Account')->find('first', array('conditions'=>array('id'=>$not[$i]['future_tweet']['account_id']))); 
        $this->set('acc',$acc);
        
        }
    }
    
    public function beforeFilter() {
        Security::setHash('md5');

        $this->Auth->allow('login', 'addUser','logout', 'changePassword');
        $this->Auth->authorize = 'controller';

        if ($this->action == 'login') {
            if ($this->Auth->loggedIn()) {
                $this->Session->setFlash('you are already logged in');
                $this->redirect(array('controller' => 'accounts', 'action' => 'index'));
            }
        }
    }

    public function isAuthorized($user) {
        // $this->set('admin', ClassRegistry::init('User')->isAdmin($this->Auth->user()));
        return true;
    }

}
